package leetcode;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 东莞呵呵
 * Date:2022-07-15
 * Time:17:53
 */
public class MyCircularQueue {
    public int useSize;
    public int[] elem;
    public int begin;
    public int end;
    public MyCircularQueue(int k) {
        this.elem=new int[k];
    }

    public boolean enQueue(int value) {
        if (isFull()){
            return false;
        }
        this.elem[end]=value;
        this.useSize++;
        this.end=(end+1)%this.elem.length;
        return true;
    }

    public boolean deQueue() {
        if(isEmpty()){
            return false;
        }
        this.useSize--;
        this.begin=(this.begin+1)%this.elem.length;
        return true;
    }

    public int Front() {
        if(isEmpty()){
            return -1;
        }
        return this.elem[begin];
    }

    public int Rear() {
        if(isEmpty()){
            return -1;
        }
        return this.elem[(end+this.elem.length-1)%this.elem.length];
    }

    public boolean isEmpty() {
        return this.useSize==0;
    }

    public boolean isFull() {
        return this.useSize==this.elem.length;
    }

    public static void main(String[] args) {
        MyCircularQueue stack=new MyCircularQueue(3);
        stack.enQueue(1);
        stack.enQueue(2);
        stack.enQueue(3);
        System.out.println(stack.Front());
        System.out.println(stack.Rear());

        stack.deQueue();
        System.out.println(stack.Front());
        System.out.println(stack.Rear());
    }
}
